﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace CLEX.Service.DataContracts
{
    [DataContract]
    public class DatabaseData
    {
        private static readonly IEqualityComparer<DatabaseData> valueComparer = new ColumnDataValueComparer();
        [DataMember]
        private readonly int databaseId;
        [DataMember]
        private readonly int userId;
        [DataMember]
        private readonly string connectionString;

        public static IEqualityComparer<DatabaseData> ValueComparer
        {
            get { return DatabaseData.valueComparer; }
        } 

        public int DatabaseId
        {
            get { return databaseId; }
        }

        public int UserId
        {
            get { return userId; }
        } 

        public string ConnectionString
        {
            get { return connectionString; }
        }

        private DatabaseData()
        {

        }

        public DatabaseData(int databaseId, int userId, string connectionString)
        {
            this.databaseId = databaseId;
            this.userId = userId;
            this.connectionString = connectionString;
        }

        public override string ToString()
        {
            return string.Format("DatabaseId: {0}, UserId: {1}, ConnectionString: {2}", DatabaseId, UserId, ConnectionString);
        }

        private sealed class ColumnDataValueComparer : IEqualityComparer<DatabaseData>
        {
            public bool Equals(DatabaseData x, DatabaseData y)
            {
                if (Object.ReferenceEquals(x, y))
                {
                    return true;
                }
                if (Object.ReferenceEquals(x, null))
                {
                    return false;
                }
                if (Object.ReferenceEquals(y, null))
                {
                    return false;
                }

                return x.databaseId == y.databaseId && x.userId == y.userId && x.connectionString == y.connectionString;
            }

            public int GetHashCode(DatabaseData obj)
            {
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return obj.databaseId.GetHashCode() ^ obj.userId.GetHashCode() ^ obj.connectionString.GetHashCode();
                }
            }
        }
    }
}
